package com.xiaotao.learn.algorithm.permute;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

class FullPermute {
    List<List<Integer>> res = new LinkedList<>();
    LinkedList<Integer> tmp = new LinkedList<>();
    boolean[] visited;
    public void dfs(int[] nums) {
        if(tmp.size() == nums.length) {
            res.add(new ArrayList<>(tmp));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (visited[i]) continue;
            tmp.add(nums[i]);
            visited[i] = true;
            dfs(nums);
            visited[i] = false;
            tmp.removeLast();
        }

    }
    public List<List<Integer>> permute(int[] nums) {
        visited = new boolean[nums.length];
        dfs(nums);
        return res;
    }
}

public class FullPermuteTest {
    public static void main(String[] args) {
        new FullPermute().permute(new int[]{1,2,3}).forEach(e -> {
            e.forEach(System.out::print);
            System.out.println();
        });
        System.out.println("123".substring(0, 1));
    }
}
